//
//  LXViolationApplyViewController.m
//  ZuLinProject
//
//  Created by bancheng on 2021/12/24.
//  Copyright © 2021 LX_App. All rights reserved.
//

#import "LXViolationApplyViewController.h"
#import "LXTableViewCell.h"
#import "LXRemarkTabelViewCell.h"
#import "LXChooseCustomViewController.h"
#import "LXCarSignListViewController.h"
#import "LXViolationModel.h"
#import "LXSignContractModel.h"
#import "LXChooseCarViewViewController.h"


@interface LXViolationApplyViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic) UITableView *tableView;
@property (nonatomic) NSArray *data;
@property (nonatomic) NSMutableDictionary *loanDic;
@property (nonatomic) NSDictionary *selDic;
// 客户id
@property (nonatomic) NSString *customerId;
// 签约id
@property (nonatomic) NSString *signId;

@end

@implementation LXViolationApplyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.view.backgroundColor = White_Color;

    [self.tableView registerClass:[LXTableViewCell class] forCellReuseIdentifier:NSStringFromClass([LXTableViewCell class])];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([LXRemarkTabelViewCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];

    [self.view addSubview:self.tableView];
    
    [self loadData];
    
    if (self.type == 1) {
        [self layoutCustomNav:self.view title:@"新增违约"];
        [self setBottomViewWithText:@"确认新增"];
    }
    else {
        [self layoutCustomNav:self.view title:@"违约详情"];
        self.tableView.height = kScreenHeight - kViewTopHeight;
    }
    
   
    
    
    
}
- (void)loadData{
    
    self.loanDic  = [NSMutableDictionary dictionary];


    
    [self.loanDic setValue:self.violationModel.carNum forKey:@"carNum"];
    
    
    if (self.type == 2) {
        [self.loanDic setValue:self.violationModel.customerName forKey:@"customerName"];
        [self.loanDic setValue:self.violationModel.signDesc forKey:@"signDesc"];
        [self.loanDic setValue:[NSString toTen:self.violationModel.deposit] forKey:@"deposit"];
        [self.loanDic setValue:[NSString toTen:self.violationModel.depositDeduct] forKey:@"depositDeduct"];
        [self.loanDic setValue:self.violationModel.cause forKey:@"cause"];

    }
    
    
    self.data = @[
        @{
            @"title":ObjectIsEmpty(self.violationModel)?@"*车牌号":@"车牌号",
            @"key":@"carNum",
            @"placeHolder" : @"请选择车牌号",
            @"canEdit":@(NO),
            @"showAccess":ObjectIsEmpty(self.violationModel)?@(YES):@(NO),
            @"maxLength":@(1000),
            @"sel":ObjectIsEmpty(self.violationModel)?@"clickCarNum":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*合约记录":@"合约记录",
            @"key":@"signDesc",
            @"placeHolder" : @"请选择合约",
            @"canEdit":@(NO),
            @"showAccess":self.type==1?@(YES):@(NO),
            @"maxLength":@(8),
            @"sel":self.type==1?@"clickSignAction":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*客户":@"客户",
            @"key":@"customerName",
            @"placeHolder" : @"请选择合约记录",
            @"canEdit":@(NO),
            @"showAccess":@(NO),
            @"sel":@"",
            @"height":@(kComHeight)
        },
      
        @{
            @"title":self.type==1?@"*押金金额(元)":@"押金金额(元)",
            @"key":@"deposit",
            @"placeHolder" : @"请输入押金金额",
            @"canEdit":@(NO),
            @"showAccess":@(NO),
            @"maxLength":@(8),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        
        @{
            @"title":self.type==1?@"*扣除违约金额(元)":@"扣除违约金额(元)",
            @"key":@"depositDeduct",
            @"placeHolder" : @"请输入扣除违约金额",
            @"canEdit":self.type==1?@(YES):@(NO),
            @"showAccess":@(NO),
            @"inputType":@(NUM_DECIMAL),
            @"keyboardType":@(UIKeyboardTypeDecimalPad),
            @"maxLength":@(8),
            @"sel":@"",
            @"height":@(kComHeight)
        },
        @{
            @"title":self.type==1?@"*违约原因":@"违约原因",
            @"key":@"cause",
            @"placeHolder" : @"请输入...",
            @"height":self.type==1?@(150):@([NSString remarksHeight:self.violationModel.cause])
        },
        
        
    ];
 
    
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.data count];
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = self.data[indexPath.row];
    return [dic[@"height"] doubleValue];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    NSDictionary *dic = self.data[indexPath.row];

   
    // 备注
    if([dic[@"key"]  isEqualToString:@"cause"]){
       LXRemarkTabelViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXRemarkTabelViewCell class])];
       cell.selectionStyle = UITableViewCellSelectionStyleNone;
       [cell setClipsToBounds:YES];
       cell.textView.maxLength = 200;
       cell.remarkTitle.text = dic[@"title"];
       cell.textView.text = self.loanDic[dic[@"key"]];
        cell.textView.userInteractionEnabled = self.type==1?YES:NO;
        if(self.type!=1){
            cell.textView.wordNumLabel.text = @"";
        }
        [cell.remarkTitle addAttributes:TextAttrCorlor([UIColor redColor]) forString:@"*"];
       [cell setTextViewEdit:^(NSString * _Nonnull textStr) {
           self.loanDic[dic[@"key"]]  = textStr;
       }];
       return cell;
   }
    
    

    
    LXTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([LXTableViewCell class])];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell setClipsToBounds:YES];
    

    cell.cellDic = dic;
    cell.textFiled.hidden = NO;
    cell.subTitleLab.hidden = YES;
    cell.textFiled.text = self.loanDic[dic[@"key"]];
    
    

    

    cell.textfiledChangeBlock = ^(NSString * _Nonnull textStr) {
        self.loanDic[dic[@"key"]]  = textStr;
    };
    
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    NSDictionary *dic = self.data[indexPath.row];
    self.selDic = dic;
    NSString * selName = dic[@"sel"];
    SEL action = NSSelectorFromString(selName);
    if ([self respondsToSelector:action]) {
        [self performSelector:action withObject:nil afterDelay:0];
    }

}
#pragma mark -- 选择合约
-(void)clickSignAction{
    LXCarSignListViewController *vc = [[LXCarSignListViewController alloc] init];
    vc.carNum = self.violationModel.carNum;
    [vc setToSendValue:^(LXSignContractModel * _Nonnull model) {
        self.signId = model.signContractId;
        self.customerId = model.driverId;
        [self.loanDic setValue:model.driverName forKey:@"customerName"];
        [self.loanDic setValue:model.signDate forKey:@"signDesc"];
        [self.loanDic setValue:model.depost forKey:@"deposit"];
        [self.tableView reloadData];
    }];
    [self.navigationController pushViewController:vc animated:YES];
}



- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, kViewTopHeight, ScreenWidth, kScreenHeight - kViewTopHeight - kComHeight - 5) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor = [UIColor colorWithHexString:@"#F7F8FA"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}




#pragma mark ---- 确认新增/修改点击事件
- (void)clickBottomView:(UIButton *)sender{
    
    if (StringIsEmpty(self.loanDic[@"carNum"])) {
        showAlert(@"请选择车牌号");
        return;
    }
    
    if (StringIsEmpty(self.signId)) {
        showAlert(@"请选择合约");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"customerName"])) {
        showAlert(@"请选择客户");
        return;
    }
   
    if (StringIsEmpty(self.loanDic[@"deposit"])) {
        showAlert(@"请输入押金金额");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"depositDeduct"])) {
        showAlert(@"请输入扣除违约金额");
        return;
    }
    if (StringIsEmpty(self.loanDic[@"cause"])) {
        showAlert(@"请输入违约原因");
        return;
    }
    
    
 
    
    [Tool showAlertTitle:@"是否确认提交" msg:nil cancelTitle:@"取消" commitBtnTitle:@"确定" cancelHandlerBlock:^{
    } commitHandlerBlock:^{
        
        
        NSMutableDictionary *dic = @{

            @"carId":self.violationModel.carId, // 所属车辆id
            @"carNum":self.violationModel.carNum,// 所属车辆车牌号
            @"customerId":self.customerId,// 客户id
            @"customerName":self.self.loanDic[@"customerName"],// 客户
            @"signId":self.signId,//签约id
            @"signDesc":self.loanDic[@"signDesc"],//签约
            @"depositDeduct":self.loanDic[@"depositDeduct"],//押金扣款
            @"deposit":self.loanDic[@"deposit"],//押金
            @"cause":self.loanDic[@"cause"], //  备注
            
        }.mutableCopy;
        
        
        
        [MSNetworkManager postRevertUrl:POST_PostAddBreachcontract params:dic success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
            
            if (isSuccess) {
                showAlert(@"新增违约成功");
                kUserDefaultSetBool(YES, kRefreshStr);
                [self.navigationController popViewControllerAnimated:YES];
            }
            
        } failure:^(NSUInteger code, NSString * _Nonnull message) {
        } withShowHud:YES];
    } onVC:self];
    
 
}
#pragma mark -- 选择车牌号
- (void)clickCarNum{
    
    LXChooseCarViewViewController *vc = [[LXChooseCarViewViewController alloc] init];
    vc.type = @"12";
    vc.comfrom = 1;
    [vc setToSendValue:^(CheLiangModel * _Nonnull model) {

    
        [self.loanDic setValue:model.carNum forKey:self.selDic[@"key"]];
        
        self.violationModel = [LXViolationModel new];
        self.violationModel.carNum = model.carNum;
        self.violationModel.carId = model.ID;
        
        [self.tableView reloadData];
        
    }];
    [self pushToNextController:vc];
}

@end
